import 'package:flutter/material.dart';


class ComplexScroll extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: MyHomePage(),
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  ScrollController _scrollController = ScrollController();
  double _containerHeight = 200.0;
  double _childWidth = 100.0;
  double _childPosition = 0.0;

  @override
  void initState() {
    super.initState();
    _scrollController.addListener(_onScroll);
  }

  @override
  void dispose() {
    _scrollController.dispose();
    super.dispose();
  }

  void _onScroll() {
    setState(() {
      _containerHeight = 200.0 - _scrollController.offset;
      if (_containerHeight < 100.0) _containerHeight = 100.0;

      _childWidth = 100.0 + _scrollController.offset / 2;
      if (_childWidth > 150.0) _childWidth = 150.0;

      _childPosition = _scrollController.offset / 2;
      if (_childPosition > 50.0) _childPosition = 50.0;
    });
  }

  @override
  Widget build(BuildContext context) {
    return CustomScrollView(
      controller: _scrollController,
      slivers: [
        SliverAppBar(
          // floating: true,
          // pinned: true,
          expandedHeight: 300.0,
          flexibleSpace: FlexibleSpaceBar(
            background: AnimatedContainer(
              duration: Duration(milliseconds: 300),
              height: _containerHeight,
              color: Colors.blue,
              child: Stack(
                children: [
                  Positioned(
                    left: _childPosition,
                    top: _childPosition,
                    child: AnimatedContainer(
                      duration: Duration(milliseconds: 300),
                      width: _childWidth,
                      height: 50.0,
                      color: Colors.red,
                    ),
                  ),
                ],
              ),
            ),
          ),
        ),
        SliverList(
          delegate: SliverChildBuilderDelegate(
            (context, index) => ListTile(
              title: Text('Item #$index'),
            ),
            childCount: 50,
          ),
        ),
      ],
    );
  }
}